/**
 * Created by DevilsEyes on 2016/4/22.
 */
//studentList
define(['layer', 'ex'], function (layer, ex) {
    'use strict';

    var OriginList = [];
    var vm = avalon.define({
        $id: 'homeworkView',
        list: [],
        title: '',
        _class: '',
        myfilter: 0,

        __unwatch__: null,

        getList: function (id) {

            $.ajax({
                url: '/api/admin/homework/',
                method: 'GET',
                data: {
                    _id: id
                },
                success: function (data) {
                    console.log(data);

                    typeof vm.__unwatch__ == 'function' && vm.__unwatch__();
                    vm.myfilter = 0;
                    vm.__unwatch__ = vm.$watch('myfilter', function (n, o) {
                        switch (vm.myfilter) {
                            case 0:
                            {
                                return vm.list = ex.copy(OriginList);
                            }
                            case -1:
                            {
                                return vm.list = ex.copy(OriginList.filter(function (s) {
                                    return s.homeworks.length == 0;
                                }));
                            }
                            case 1:
                            {
                                return vm.list = ex.copy(OriginList.filter(function (s) {
                                    return s.homeworks.length > 0;
                                }));
                            }
                        }
                    });

                    if (data.code == 0) {

                        vm._class = data.info.class;
                        vm.title = data.info.title;

                        OriginList = data.students
                            .map(function (s) {
                                s.homeworks = [];
                                return s;
                            })
                            .sort(function (a, b) {
                                return a.id > b.id;
                            });

                        var homeworks = data.info.files || [];
                        homeworks.map(function (h) {
                            OriginList.some(function (s) {
                                if (s.id == h.id) {
                                    s.homeworks.push(h);
                                    return true;
                                }
                            })
                        });

                        vm.list = ex.copy(OriginList);

                    } else {
                        layer.msg(data.msg);
                    }


                }
            })
        },
        //辅助方法
        formatSize: function (size) {
            var unit = ['B', 'KB', 'MB'];
            var res = '';
            if (unit.some(function (unit) {
                    if (size < 1024) {
                        return res = ('' + size).slice(0, 4).replace(/\.$/, '') + unit;
                    }
                    size = size / 1024;
                })) {
                return res;
            } else {
                return 'to Large';
            }
        }
    });

    return {
        init: function (id) {
            vm.list = [];
            vm.getList(id);
            // $root.page = temp;
        }
    }
});
